package com.yixun.lbx.controller.farm;

import com.yixun.controller.BaseController;
import com.yixun.lbx.entity.YxProBreedTreat;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.service.BreedTreatService;
import com.yixun.lbx.service.CompanyService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/bt")
public class BreedTreatController extends BaseController {
    private final BreedTreatService breedTreatService;
    private final CompanyService companyService;

    public BreedTreatController(CompanyService companyService, BreedTreatService breedTreatService) {
        this.companyService = companyService;
        this.breedTreatService = breedTreatService;
    }

    /*
     *获取诊疗记录列表
     */
    @PostMapping("/getListByPage")
    public HashMap<String, Object> getListByPage(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        Integer count = breedTreatService.getCount(map);
        List<YxProBreedTreat> BreedTreats = breedTreatService.getListByPage(map);

        map.put("count", count);
        map.put("BreedTreats", BreedTreats);
        return map;
    }

    /*
     *添加诊疗记录
     */
    @PostMapping("/addBreedTreat")
    public int addBreedTreat(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        map.put("addUser", this.getSessionUser().getUser_id());
        return breedTreatService.addBreedTreat(map);
    }

    /*
     *修改诊疗记录
     */
    @PostMapping("/updateBreedTreat")
    public int updateBreedTreat(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] beeIds = map.get("beeId").toString().split(",");
        map.put("beeIds", beeIds);
        return breedTreatService.updateBreedTreat(map);
    }

    /*
     *淘汰诊疗记录
     */
    @PostMapping("/deleteBreedTreat")
    public int deleteBreedTreat(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] ids = map.get("id").toString().split(",");
        map.put("ids", ids);
        return breedTreatService.deleteBreedTreat(map);
    }

    @GetMapping("/getObjByArgs")
    public List<YxProBreedTreat> getObjByArgs(YxProBreedTreat breedTreat) {
        return breedTreatService.getObjByArgs(breedTreat);
    }

    /*搜索*/
    @RequestMapping("/getSearchListByPage")
    public HashMap<String, Object> getSearchListByPage(@RequestBody HashMap<String, String> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", String.valueOf(qyId));
        }
        List<YxProBreedTreat> objs = breedTreatService.getSearchListByPage(map);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("obj", objs);
        hashMap.put("count", objs.size());
        return hashMap;
    }

    private Long getQyId() {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(this.getSessionUser().getUser_name());
        YxProCompany company = companyService.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            return company.getQyId();
        } else {
            return 0L;
        }
    }
}
